//
//  ProjectListController.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/24/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "ProjectListController.h"
#import "Project.h"
#import "Dept.h"
#import "HRStore.h"
#import "ProjectDetailViewController.h"

@interface ProjectListController ()

@end

@implementation ProjectListController
@synthesize dataSource, nc;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"project";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    Project *project = [self.dataSource objectAtIndex:indexPath.row];
    Dept *dept = [[HRStore sharedStore] findDeptById:project.deptId];
    

    cell.textLabel.text = project.projectFullName;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ (%@)", project.projectShortName, dept.deptShortName];
    
    cell.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Project *project = [dataSource objectAtIndex:indexPath.row];
    ProjectDetailViewController *detailController = [[ProjectDetailViewController alloc] init];
    [detailController setProjectId:project.projectId];
    NSString *title = [NSString stringWithFormat:@"Project Resource Allocation - %@", [tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text];
    [detailController.navigationItem  setTitle:title];
    [nc pushViewController:detailController animated:YES];
}

@end
